const app = getApp();
var avUtil = require('../../utils/av-util.js');

const bulletin = require('../../antiqueData/bulletinData.js');



Page({
  /**
   * 页面的初始数据
   */
  data: {
    ispublishContent: false,
    isannouncement: true,
    isSelected: false,
    isFreed: false,
    isPayed: false,
    submitting: false,
    isClassify: true,
    isDemand: true,
    selectedType: '',
    selectedDemand: '',
    kindName: '',
    images: [],
    type:[
      {
        "kid": 0,
        "kindName": "云翠珠宝"
      },
      {
        "kid": 1,
        "kindName": "紫砂陶器"
      },
      {
        "kid": 2,
        "kindName": "书画篆刻"
      },
      {
        "kid": 3,
        "kindName": "古币银元"
      },
      {
        "kid": 4,
        "kindName": "文玩杂项"
      },
      {
        "kid": 5,
        "kindName": "茶酒木艺"
      },
      {
        "kid": 6,
        "kindName": "铜器铁器"
      },
      {
        "kid": 7,
        "kindName": "硬币纸品"
      },
    ],
    demand:[
      {
        "kid": 0,
        "kindName": "求点评"
      },
      {
        "kid": 1,
        "kindName": "求开价"
      },
      {
        "kid": 2,
        "kindName": "求收购"
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var bulletinContent = bulletin.bulletinData;
    this.setData({
        bulletinContent: bulletinContent,
        src:"",
        isSrc:false,
        ishide:"0",
        autoFocus:true,
        isLoading:false,
        loading:true,
        isdisabled:false
    })
  },

  addImage: function(){
    var that = this;
    wx.chooseImage({
        count: 9 - that.data.images.length,
        success: function(obj){
            console.log("success");
            that.data.images = that.data.images.concat(obj.tempFilePaths);
            that.setData({
                images: that.data.images
            });
        }
    });
  },

  bindTypeChange: function(e) {
    this.setData({
        selectedType: e.detail.value
    });
  },

  errBox: function(err){
    wx.showModal({
      title: '错误',
      content: err,
      showCancel: false
    });
  },
  msgBox: function(msg){
    wx.showModal({
        title: '信息',
        content: msg,
        showCancel: false
    });
  },

  chooseClassify: function(){
    this.setData({
      isClassify: !this.data.isClassify
    })
  },

  chooseDemand: function(){
    this.setData({
      isDemand: !this.data.isDemand
    })
  },


  tapKind: function(event){
    var that = this;
    this.setData({
      current: event.currentTarget.dataset.current,
      selectedType: event.currentTarget.dataset.current,
      kindName: this.data.type[event.currentTarget.dataset.current].kindName,
    })
    setTimeout(function(){
      that.setData({
        isClassify: !that.data.isClassify
      })
    },500)
  },

  tapdemand: function(event){
    var that = this;
    this.setData({
      current: event.currentTarget.dataset.current,
      selectedDemand: event.currentTarget.dataset.current,
      kindName: this.data.type[event.currentTarget.dataset.current].kindName,
    })
    setTimeout(function(){
      that.setData({
        isDemand: !that.data.isDemand
      })
    },500)
  },



  doSubmitPassage: function(e){
    this.setData({
        submitting: true
    });
    var data = this.data;
    var that = this;
      avUtil.submitPassage(
        e.detail.value.textarea, 
        data.images, 
        data.type[data.selectedType],
        function(pa){
          that.setData({
              submitting: false
          });
          wx.showModal({
              title: '成功',
              content: "提交成功",
              showCancel: false,
              success: function(res) {
              if (res.confirm) {
                console.log(pa);
                wx.redirectTo({
                    url: '../detail/detail?id=' + pa.id
                })
              }
            }
          });
        },
        function(error) {
            that.setData({
                submitting: false
            });
            that.errBox("提交失败");
            console.error(error);
        })
    },
  submitPassage: function(e){
      if (e.detail.value.textarea === '')
      {
          wx.showModal({
              title: '提示',
              content: '请输入要发布的内容',
              showCancel: false
          });
          return;
      }
      var that = this;
      wx.showModal({
          title: '提示',
          content: '确定发布吗？',
          success: function(res) {
              if (res.confirm)
                  that.doSubmitPassage(e);
          }
      });
  },



  onReady: function(){
    wx.hideToast()
  },

  isCheck: function(){
    this.setData({
      isSelected: !this.data.isSelected
    })
  },

  isFree: function(){
    this.setData({
      isFreed: !this.data.isFreed
    })
  },

  isPay: function(){
    this.setData({
      isPayed: !this.data.isPayed
    })
  },
  


  // clearPic:function(){//删除图片
  //   var that = this;
  //   that.setData({
  //     isSrc:false,
  //     src:""
  //   })
  // },



  // 公告提示
  iknow: function(){
    this.setData({
      isannouncement: false,
      ispublishContent: true
    })
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})